import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/config/constant.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/page/home/convenient/repair/repair_details_page.dart';
import 'package:kkhousekeep/page/home/convenient/repair/repair_result_page.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../../../../models/repair_order_entity.dart';
import '../../../../net/home_repair_connect.dart';

class RepairHomeController extends GetxController {
  var tabs = <TabModel>[
    TabModel(0, "待维修"),
    TabModel(1, "已完成"),
  ];

  var tabId = 0.obs;
  var pendingList = <RepairOrderObj>[].obs;
  var finishList = <RepairOrderObj>[].obs;

  late HomeRepairConnect _connect;

  @override
  void onInit() {
    _connect = Get.put(HomeRepairConnect());
    loadNetworkData();
    super.onInit();
  }

  void performClickTab(int tabId) {
    if (tabId == this.tabId.value) {
      return;
    }
    this.tabId.value = tabId;
    if (tabId == 0 && pendingList.isEmpty || tabId == 1 && finishList.isEmpty) {
      loadNetworkData();
    }
  }

  void performClickItem(int orderId) {
    var arguments = {ArgumentKey.orderId: orderId};
    Get.to(
      () => const RepairDetailsPage(),
      arguments: arguments,
    )?.whenComplete(() => loadNetworkData());
  }

  void performClickGotoDisposeResult(int orderId) {
    var arguments = {ArgumentKey.orderId: orderId};
    Get.to(
      () => const RepairResultPage(),
      arguments: arguments,
    )?.whenComplete(() => loadNetworkData());
  }

  void performClickDialogSubmit({
    required int orderId,
    int responseSpeed = 0,
    int serviceAttitude = 0,
    int constructionQuality = 0,
  }) async {
    var response = await _connect.appraiseOrder(
      orderId: orderId,
      responseSpeed: responseSpeed,
      serviceAttitude: serviceAttitude,
      constructionQuality: constructionQuality,
    );
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      int index = finishList.indexWhere((element) => element.id == orderId);
      if (index != -1) {
        finishList[index].state = 'evaluated';
        finishList.refresh();
      }
      SmartDialog.dismiss();
      return;
    }
    DialogUtil.showNetworkFail();
  }

  void loadNetworkData() async {
    List<RepairOrderObj> dataList =
        await _connect.getRepairOrderList(tabId: tabId.value);

    if (tabId.value == 0) {
      pendingList.value = dataList;
      return;
    }
    finishList.value = dataList;
  }
}

class TabModel {
  int id;
  String title;

  TabModel(
    this.id,
    this.title,
  );
}

///处理进度类型
enum ProgressState {
  pending(
    state: 'pending',
    name: '待处理',
    iconName: 'home_convenient_repair_details_todo.png',
  ),
  processing(
    state: 'processing',
    name: '处理中',
    iconName: 'home_convenient_repair_details_doing.png',
  ),
  completed(
    state: 'completed',
    name: '已完成',
    iconName: 'home_convenient_repair_details_finish.png',
  );

  final String state;
  final String name;
  final String iconName;

  const ProgressState({
    required this.state,
    required this.name,
    required this.iconName,
  });

  static ProgressState getProgressStateByState(String state) {
    return ProgressState.values
            .firstWhereOrNull((element) => element.state == state) ??
        ProgressState.completed;
  }
}
